<?php
class ymdian_orderControl extends skymvc{
	public $user;
	public $userid;
	public $ymdian_order_type=1;
	public $oc_where;
	public function __construct(){
		parent::__construct();
		
		
	}
	
	public function onInit(){
		$this->loadConfig("table");
		
		M("login")->checkLogin();
		$this->userid=M("login")->userid;
		$this->user=M("login")->getUser();
		
		$this->oc_where=M("login")->userid?" (userid=".M("login")->userid." or oc_ssid='".OC_SSID."')  ":"oc_ssid='".OC_SSID."' ";
		$this->oc_where.=" AND siteid=".SITEID;
	}
	
	public function onDefault(){
		
	}
	
	public function onConfirm(){
		 
		$user=$this->user;
		$shopid=get_post('shopid','i');
		$ymdian=M("ymdian")->selectRow("id=".$shopid);
		if(!$ymdian){
			$this->goALl("夜猫店不存在",1);
		}
		$where=$this->oc_where." AND shopid=".$shopid;
		$ymdian_orderCart=M('ymdian_order_cart')->select(array("where"=>$where));
		if(empty($ymdian_orderCart)) $this->goall("请先去选购产品",1,0,"/index.php");
		//收货地址
		//不在营业中 
		$opentype=shopopen($ymdian['yystart'],$ymdian['yyend']);
		if($opentype!="doing"){
			$this->goall("夜猫店已休息了",1);
		}
		foreach($ymdian_orderCart as $v){				
			$object_ids[]=$v['object_id'];
			$productids[]=$v['productid'];
			$ksids[]=$v['ksid'];
		}
		if($ymdian_orderCart){
				$products=M('product')->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
		}
		
		$total_money=0; 
		$total_num=0;
			foreach($ymdian_orderCart as $k=>$v){
			 	
				$product=$products[$v['productid']]; 
				$v['title']=$product['title'];
				$v['imgurl']=images_site($product['imgurl']);
				if(isset($kslist[$v['ksid']])){
					$ks=$kslist[$v['ksid']];
					$v['price']=$product['lower_price']>0?$product['lower_price']:$ks['price'];
					$v['ks_title']=$ks['title'];
				}else{
					$v['price']=$product['lower_price']>0?$product['lower_price']:$product['price'];
				}
				$total_money+=$v['price']*$v['amount'];
				$v['total_money']=$v['price']*$v['amount'];
				$total_num+=$v['amount'];
				$ymdian_orderCart[$k]=$v;
			}
		
		$address=M("user_address")->select(array("where"=>" siteid=".SITEID." AND status<10 AND userid=".$user['userid'],"shop_order"=>"isdefault desc","limit"=>10));
		if($address){
			foreach($address as $v){
				$d_ids[]=$v['province_id'];
				$d_ids[]=$v['city_id'];
				$d_ids[]=$v['town_id'];
			}
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
		}
		if($total_money<$ymdian['qsjia']){
			$this->goALl("商品价格小于起送价".$ymdian['qsjia']."元，不能下单哦");
		}
		$moneypay=0;	
		if($user['money']>=$total_money){
			$moneypay=1;
		}
		$bdata=array(
			"address"=>$address,
			"dist_list"=>$dist_list,
			"back_url"=>$_SERVER['HTTP_REFERER'],
			"orderCart"=>$ymdian_orderCart,
			"ymdian"=>$ymdian,
			"total_money"=>$total_money,
			"total_num"=>$total_num,
			"user"=>$user,
			"moneypay"=>$moneypay, 
			"pay_type_list"=>pay_type_list($moneypay),
		);
		if(get('ajax')){
			$this->goALl("success",0,$bdata);
		}
		$this->smarty->assign($bdata);
		$this->smarty->display("ymdian_order/confirm.html");
		
	}
	

	
	public function onorder(){
		
		$user=$this->user;
		$shopid=post('shopid','i');
		$user_address_id=post('user_address_id','i');
		$addr=M("user_address")->selectRow(array("where"=>"id=".$user_address_id));
		if(empty($addr) or empty($addr['telephone'])){
			$this->goall("请完善收货地址",1);
		}
		
		//处理商品
		$ids=post('cart_id','i');
		$discount_money=0;
		if($ids){
			$cart_list=M("ymdian_order_cart")->select(array("where"=> $this->oc_where ." AND id in("._implode($ids).") "));
			 
			if(empty($cart_list)) $this->goall("请选择要购买的商品",1);
			$total_money=0;
			$ymdians=array();
			foreach($cart_list as $v){				
				$object_ids[]=$v['object_id'];
				$ksids[]=$v['ksid'];
				$productids[]=$v['productid'];
			}
			
			if($cart_list){
				$products=M('product')->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
				$ymproducts=M('ymdian_product')->id_list(array("where"=>" id in("._implode($object_ids).") "));
			}
			foreach($cart_list as $k=>$v){
				$product=$products[$v['productid']];
				$ymproduct=$ymproducts[$v['object_id']];
				if(empty($product)){
					$this->goall("产品".$product['title']."已下线，请删除该商品",1);
				}
				if($ymproduct['total_num']<$v['amount']){
					$this->goall("产品<a href='".R("/index.php?m=show&id=".$product['id'])."'>".$product['title']."</a>库存不足，当前还剩".($ymproduct['total_num'])."件，请删除该商品",1);
				}
				$v['price']=$product['lower_price']>0?$product['lower_price']:$product['price'];
				$total_money +=$v['price']*$v['amount'];
				if($product['lower_price']>0){
					$discount_money+=($product['price']-$product['lower_price'])*$v['amount'];
				}
				$cart_list[$k]=$v;
			}
			
			
			
		}else{
			$this->goall("请选择要购买的商品",1);
		}
		$ymdian=M("ymdian")->selectRow("id=".$shopid);
		if($total_money<$ymdian['qsjia']){
			$this->goALl("商品价格小于起送价".$ymdian['qsjia']."元，不能下单哦");
		}
		//不在营业中 
		$opentype=shopopen($ymdian['yystart'],$ymdian['yyend']);
		if($opentype!="doing"){
			$this->goall("夜猫店已休息了",1);
		}
		//End 处理商品
		
		$isfinish=0;
		$ispay=1;
		$this->loadControl("jfapi");
		$goods_money=$total_money;
		$express_money=EXPRESS_FEE;
		$total_money=$total_money+$express_money;
		/* Start优惠券 */
		M("ymdian_order")->begin();
		//活动类型
		$hdtype=0;
		//清除购物车
		M("ymdian_order_cart")->delete($this->oc_where." AND id in("._implode($ids).")");
		$coupon_id=post('coupon_id','i');
		if($coupon_id){
	 
			$coupon=M("coupon")->selectRow("id=".$coupon_id." AND siteid=".SITEID);
			if(!in_array($coupon['type_id'],array(1,2)) or $goods_money<$coupon['lower_money']){
				M("ymdian_order")->rollback();
				$this->goAll("当前优惠券不能使用",1,$coupon);
			}
			$coupon_user=M("coupon_user")->selectRow("coupon_id=".$coupon_id." AND status=0 AND userid=".M("login")->userid);			
			if($coupon && $coupon_user){
				$coupon_money=$coupon['money'];
				M("coupon_user")->update(array("status"=>1),"id=".$coupon_user['id']);
				M("coupon")->update(array("use_num"=>$coupon['use_num']+1),"id=".$coupon_id);
				$total_money=$total_money-$coupon_money;
			}else{
				$coupon_id=0;
			}						
		}else{
			$coupon_money=0;
		}
		/* End 优惠券*/
		
		//处理余额消费
		$pay_type=get_post('pay_type','h');
		if($pay_type=='moneypay'){
			if($total_money>$user['money']){
				$this->goALL("余额不足",1);
			}else{
				//添加金钱消费记录		
				M("user")->addMoney(array(
					"userid"=>M("login")->userid,
					"money"=>-$total_money,
					"content"=>"您购买了产品花了[money]元",	
				));
				$ispay=2;
				$isfinish=1;
			}
		}
		
		if($pay_type=='unpay'){
			$isfinish=1;
			$unpay=1;
		}
		 /****首单优惠***/
		 
		 if(ORDER_FIRST_DISCOUNT>0){
			 $firstorder=M("ymdian_order")->selectRow("userid=".M("login")->userid." AND status<4 ");
			 if(empty($firstorder) && $total_money>=ORDER_FIRST_MONEY){
			 	$total_money-=ORDER_FIRST_DISCOUNT;
				$discount_money+=ORDER_FIRST_DISCOUNT;
				$hdtype=1;
			 }
		 }
		 /****END 收单优惠*****/
			//处理订单
			$orderno="ym".date("YmdHis").$user['userid'];
			$comment=post('comment','h');
			$order_id=M("ymdian_order")->insert(array(
				"orderno"=>$orderno,
				"dateline"=>time(),
				"ispay"=>$ispay,
				"userid"=>$user['userid'],
				"send_id"=>post('send_id','i'),
				"comment"=>$comment,
				"money"=>$total_money,
				"express_money"=>$express_money,
				"goods_money"=>$goods_money,
				"discount_money"=>$discount_money,
				"user_address_id"=>$user_address_id,
				"object_id"=>0,
				"unpay"=>$unpay,
				"shopid"=>$shopid,
				"coupon_id"=>$coupon_id,
				"coupon_money"=>$coupon_money,
				"sushe"=>$addr['sushe'],
				"sc_id"=>$addr['city_id'],
				"siteid"=>SITEID,
				"paytype"=>$pay_type,
				"hdtype"=>$hdtype
			));
			//处理订单的产品
			$prostr="\n\r";
			$printer_products=array();
			if($order_id){
				foreach($cart_list as $k=>$v){
					//打印产品信息
					$printer_products[]=array(
						"title"=>$products[$v['productid']]['title'],
						"price"=>$v['price'],
						"amount"=>$v['amount'],
						"money"=>$v['price']*$v['amount'],
					);
					$prostr.= $products[$v['productid']]['title']."x".$v['amount'].", \n\r ";
					M("ymdian_order_product")->insert(array(
						"order_id"=>$order_id,
						"userid"=>$user['userid'],
						"object_id"=>$v['object_id'],
						"productid"=>$v['productid'],
						"type_id"=>$v['type_id'],
						"price"=>$v['price'],
						"amount"=>$v['amount'],
						"dateline"=>time(),
						"ksid"=>$v['ksid']
					));
					//减掉商品库存
					M("ymdian_product")->total_num_add(array(
						"shopid"=>$v['shopid'],
						"productid"=>$v['productid'],
						"total_num"=>-$v['amount'],
						"content"=>"[下单]"
					));
					
				}
			}else{
				M("ymdian_order")->rollback();
				$this->goall("订单处理失败",1,0,$back_url);
			}
			
			//处理订单收货地址
			$d_ids=array($addr['province_id'],$addr['city_id'],$addr['town_id']);
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
			M("ymdian_order_address")->insert(array(
				"order_id"=>$order_id,
				"userid"=>$user['userid'],
				"truename"=>$addr['truename'],
				"telephone"=>$addr['telephone'],
				"p_c_t"=>$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']],
				"address"=>$addr['address'],
				"dateline"=>time(),
				"sushe"=>$addr['sushe']
			));
		 	$prostr.=$addr['truename'].",".$addr['telephone'].",".$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']].",宿舍".$addr['sushe'];
			//打印收货地址
			$address=$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']];
			$site=M("sites")->selectRow("siteid=".SITEID);
			$printer_address=array(
				"address"=>$site['sitename']." ".$address."宿舍".$addr['sushe'],
				"telephone"=>$addr['telephone'],
				"nickname"=>$addr['truename']
			);
		 
		
		//发送推送信息
		$content=date("H:i:s")."夜猫店新订单，详情：".$prostr;
		M("apppush")->sendShop(array(
				"table"=>"ymdian",
				"content"=>$content,
				"shopid"=>$shopid,
		));
		//End 推送信息
		//手机短信
		if(SMS_YMDIAN_ORDER_NEW){
			$smsdata=array(
				"key"=>"order",
				"para"=>array($orderno."(夜猫店 ".date("Y-m-d H:i:s")."详情：".$prostr.")","",""),
				"content"=>"【".SMS_QIANMING."】".$content
			);
			$ymdian_admins=M("ymdian_admin")->select(array("where"=>" shopid=".$shopid));
			if($ymdian_admins){
				 
				foreach($ymdian_admins as $y){
					if(is_tel($y['telephone'])){
						M("smspush")->insert(array(
							"telephone"=>$y['telephone'],
							"dateline"=>time(),
							"content"=>base64_encode(serialize($smsdata))
						));
					}
				}
			}
		}
		/*处理 店铺客户*/
		$shop_user=M("ymdian_user")->selectRow(" shopid=".$shopid." AND userid=".M("login")->userid." ");
		if(empty($shop_user)){
			M("ymdian_user")->insert(array(
				"shopid"=>$shopid,
				"userid"=>M("login")->userid,
				"dateline"=>time(),
				"kday"=>date("Y-m-d")
			));
		}
		/**/
		M("ymdian_order")->commit();
		$data=array(
			"order_id"=>$order_id,
			"pay_type"=>$pay_type,
			"ispay"=>$ispay
		);
		
		/****打印服务****/
		if(HAS_PRINTER==1){
			if($isfinish){
				$printer_isvalid=1;
			}else{
				$printer_isvalid=0;
			}
			$pays=pay_type_list(1);
			$printer_order=array(
				"money"=>$total_money,
				"dateline"=>time(),
				"pay_type"=>$pays[$pay_type],
				"orderno"=>$orderno,
				"comment"=>$comment
			);
			
			$printer=M("mod_printer")->selectRow("tablename='ymdian' AND bstatus=2 AND shopid=".$shopid);
			$printer_content=array(
				"products"=>$printer_products,
				"addr"=>$printer_address,
				"order"=>$printer_order,
			);
			$printer_content=base64_encode(json_encode($printer_content));
			if($printer ){
				M("mod_printer_plan")->insert(array(
					"tablename"=>"ymdian",
					"shopid"=>$shopid,
					"title"=>"订单".$orderno,
					"dateline"=>time(),
					"last_time"=>time(),
					"ftable"=>"ymdian_order",
					"fid"=>$order_id,
					"content"=>$printer_content,
					"isvalid"=>$printer_isvalid
				));
			}
			}
	/*****End打印服务**********/
	
	
		if($isfinish){
			$this->goall($this->lang['ymdian_order_success'],0,$data,"/index.php?m=ymdian_order&a=show&order_id=".$order_id);
		}else{
			//header("Location: /index.php?m=ymdian_order&a=pay&pay_type=".$pay_type."&order_id=".$order_id);
			$this->goall("下单成功",0,$data,"/index.php?m=ymdian_order&a=pay&pay_type=".$pay_type."&order_id=".$order_id);
		}
	}
	
	public function onPay(){
		$order_id=get('order_id','i');
		$pay_type=get('pay_type','h');
		$this->smarty->assign(array(
			"payurl"=>"/index.php?m=recharge&a=default&table=ymdian&pay_type=".$pay_type."&order_id=".$order_id,
			"order"=>M("ymdian_order")->selectRow("order_id=".$order_id)
		));
		//$this->smarty->display("ymdian_order/pay.html");
		header("Location: /index.php?m=recharge&a=default&table=ymdian&pay_type=".$pay_type."&order_id=".$order_id);
	}
	
	public function onMy(){
		$where="   status<99 AND userid=".$this->userid;
		$url=APPINDEX."?m=ymdian_order&a=my";
		switch(get('t')){
			case "comment":
				$url.="&t=comment";
				$where.=" AND status=3 AND iscomment=0";
				break;
		}
		$start=get('per_page','i');
		$limit=20;
		$status=get('status','i');
		if($status){
			$where.=" AND status=".($status-1);
			$url.="&status=".$status;
		}
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("ymdian_order")->select($option,$rscount);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("ymdian_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"ymdian_order"=>"id DESC"));
			 
				$v['products']=$this->orderproduct($v['order_id']);
				$num=$money=0;
				$order_products=M("ymdian_order_product")->select(array(
					"where"=>" order_id=".$v['order_id'],
				));
				if($order_products){
					foreach($order_products as $p){
						$num+=$p['amount'];
						 
					}
				}
				$v['num']=$num; 
				 $v['status_name']=$order_status_list[$v['status']];
				if($v['paytype']!='unpay'){
					$v['ispay_name']=$order_ispay[$v['ispay']];
				}else{
					$v['ispay_name']="货到付款";
				}
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		
		$this->smarty->goassign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("ymdian_order/my.html");
	}
	

	
	public function onshow(){
		 
		$order_id=get('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['userid']!=M("login")->userid){
			$this->goall("该订单不是您的",1);
		}
		$shop=M("ymdian")->selectRow("id=".$data['shopid']);
		$addr=M("ymdian_order_address")->selectRow(array("where"=>"order_id=".$order_id,"ymdian_order"=>"id DESC"));
		 
		$ymdian_order_status_list=$this->config_item('order_status_list');
		
		$ymdian_order_type_list=$this->config_item('order_type_list');
		$ymdian_order_ispay=$this->config_item('order_ispay');
		//获取商品
		 
		$ymdian_order_product=M('ymdian_order_product')->select(array("where"=>"order_id=".$data['order_id']));
		$total_num=0; 
		foreach($ymdian_order_product as $k=>$v){
			$p=M('product')->selectRow(array("where"=>"id=".$v['productid']));
			$p['order_price']=$v['price'];
			$p['iscomment']=$v['iscomment'];
			$p['rating_grade']=$v['rating_grade'];
			$p['amount']=$v['amount'];
			$total_num+=$v['amount'];
			$ks=M('product_ks')->selectRow("id=".$v['ksid']);
			if($ks){
				$p['ks_title']=$ks['title'];
				$p['price']=$ks['price'];
			}
			$ymdian_order_product[$k]=$p;
		}
		 
		$this->smarty->goassign(array(
			"data"=>$data,
			"shop"=>$shop,
			"addr"=>$addr,
			"order_status_list"=>$ymdian_order_status_list,
			"order_type_list"=>$ymdian_order_type_list,
			"order_ispay"=>$ymdian_order_ispay,
			"total_num"=>$total_num,
			"order_product"=>$ymdian_order_product,
		));
		$this->smarty->display("ymdian_order/show.html");
	}
	
	public function orderproduct($order_id){
		
		$order_product=M("ymdian_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['ymdian_order_price']=$v['price'];
				$p['amount']=$v['amount'];
				
				$order_product[$k]=$p;
			}
		}
		return $order_product;	
	}
	
	public function onReceive(){
		$order_id=get('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>" order_id=".$order_id." AND userid=".$this->userid." "));
		if(empty($data) or $data['status']<2){
			$this->goall($this->lang['data_no_exists'],1,0,"/index.php");
		}
		M("ymdian_order")->update(array("isreceived"=>2)," order_id=".$order_id." ");
		$this->goall($this->lang['save_success'],0,$data);
	}
	
	public function oncomment(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$order=M("ymdian_order")->selectRow("order_id=".$order_id);
		if($order['userid']!=M("login")->userid or $order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$shop=M("ymdian")->selectRow("id=".$order['shopid']);
		$comment=M("ymdian_order_comment")->selectRow("order_id=".$order_id);
		$this->smarty->assign(array(
			"order"=>$order,
			"shop"=>$shop,
			"comment"=>$comment
		)); 
		$this->smarty->display("ymdian_order/comment.html");
	}
	
	
	
	public function onCommentSave(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$order=M("ymdian_order")->selectRow("order_id=".$order_id);
		if($order['userid']!=M("login")->userid or $order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$comment=M("ymdian_order_comment")->selectRow("order_id=".$order_id);
		$data=M("ymdian_order_comment")->postData();
		$data['siteid']=SITEID;
		$data['shopid']=$order['shopid'];
		$data['dateline']=time();
		$data['userid']=M("login")->userid;
		if($comment){
			M("ymdian_order_comment")->update($data,"id=".$comment['id']);
		}else{
			M("ymdian_order_comment")->insert($data);
		}
		$this->goAll("评价成功",1);		
	}
	
	public function onproduct_comment(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$koudai_order=M("koudai_order")->selectRow("order_id=".$order_id);
		if($koudai_order['userid']!=$this->userid or $koudai_order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$row=M("koudai_order_product")->selectRow(" order_id=".$order_id." AND object_id=".$id." ");
		if(empty($row) or $row['iscomment'] ){
			$this->goall("产品出错",1);
		}
		$this->smarty->display("koudai_order/koudai_product_comment.html");
	}
	
	public function onproduct_comment_save(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$ymdian_order=M("ymdian_order")->selectRow("order_id=".$order_id);
		if($ymdian_order['userid']!=$this->userid or $ymdian_order['status']!=3 ){
			$this->goall("订单信息出错",1);
		}
		$row=M("ymdian_order_product")->selectRow(" order_id=".$order_id." AND object_id=".$id." ");
		if(empty($row) or $row['iscomment']){
			$this->goall("产品出错",1);
		}
		 
		$data=array(
			"grade"=>post('grade','i'),
			"userid"=>$this->userid,
			"object_id"=>$id,
			"type_id"=>1,
			"dateline"=>time(),
			"status"=>0,
			"model_id"=>MODEL_ymdian_PRODUCT_ID,
			"content"=>post('content','h'),
			"order_id"=>$order_id
		);
		M("rating")->insert($data);
		M("ymdian_order_product")->update(array("iscomment"=>1,"rating_grade"=>post('grade','i'))," order_id=".$order_id." AND object_id=".$id." ");
		$pro=M("ymdian_product")->selectRow("id=".$id);
		$rating_grade=M("rating")->selectOne(array("where"=>" model_id=".MODEL_ymdian_product_ID." AND type_id=1 AND object_id=".$id." ","fields"=>" AVG(grade) as g"));
		M("ymdian_product")->update(array("rating_grade"=>$rating_grade,"rating_num"=>$pro['rating_num']+1)," id=".$id);
		$this->goall("评论成功");
	}
	
	public function onCancel(){
		
		$order_id=get_post('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['status']>=3) $this->goall("操作失败",1);
		M("ymdian_order")->begin();
		M("ymdian_order")->update(array("status"=>10,"whocancel"=>"user"),"order_id=".$order_id);
		$content=post('content');
		$message=post('message','h');
		M("ymdian_order_log")->insert(array(
			"dateline"=>time(),
			"order_id"=>$order_id,
			"content"=>$content."，原因：".$message,
		));
		//加商品库存
		$order_product=M("ymdian_order_product")->select(array("where"=>"order_id=".$order_id));
		foreach($order_product as $v){
			M("ymdian_product")->total_num_add(array(
				"shopid"=>$data['shopid'],
				"productid"=>$v['productid'],
				"total_num"=>$v['amount'],
				"content"=>"[下单取消]"
			));
			
		}
	 	//通知商家
		$content=date("H:i:s")."您的订单".$data."被取消了。";
		M("apppush")->sendShop(array(
				"table"=>"ymdian",
				"content"=>$content,
				"shopid"=>$data['shopid'],
		));
		//退钱给用户
		if($data['ispay']==2){
			/**
			$row=M("recharge_back")->selectRow(" order_id=".$order_id." AND tablename='ymdian' ");
			if(empty($row)){
				M("recharge_back")->insert(array(
					"order_id"=>$order_id,
					"tablename"=>"ymdian",
					"paytype"=>$data['paytype'],
					"dateline"=>time(),
					"ip"=>ip(),
					"userid"=>M("login")->userid	
				));
			}
			**/
			$option=array(
					"userid"=>$data['userid'],
					"money"=>$data['money'],
					"content"=>"您的订单被取消了，退还给您￥".$data['money']."",
					"siteid"=>SITEID
				);
				M("user")->addMoney($option);
			
		}
		M("ymdian_order")->commit();
		$this->goall("取消成功");
	}
	
}
?>